package com.df.my_net_game.net.websocket;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;


import java.util.List;

/**
 * @author Admin
 * @class MsgHandle
 * @date 2024/4/3 17:11
 */
public class HandleMsg {

    /***
     * 消息发送
     * @author: Admin
     * @date: 2024/4/3 17:12
     * @param ctx channel上下文
     * @param b 消息数据
     * @param msgId 消息id
     * @return: void
     */
    public static void sendMsg(ChannelHandlerContext ctx, byte[] b, int msgId) {
        byte[] b2 = new byte[b.length + 4];
        ByteBuf respByteBuf = Unpooled.wrappedBuffer(b2);
        //从0位开始写
        respByteBuf.writerIndex(0);
        //前4位是消息id
        respByteBuf.writeInt(msgId);
        //响应数据
        respByteBuf.writeBytes(b);
        ctx.channel().writeAndFlush(new BinaryWebSocketFrame(respByteBuf));
    }
    public static void sendMsg(ChannelHandlerContext ctx, byte[] b, String url) {
        byte[] b2 = new byte[b.length + 4];
        ByteBuf respByteBuf = Unpooled.wrappedBuffer(b2);
        //从0位开始写
        respByteBuf.writerIndex(0);
        //TODO 根据Url找到msgId
        //前4位是消息id
//        respByteBuf.writeInt(msgId);
        //响应数据
        respByteBuf.writeBytes(b);
        ctx.channel().writeAndFlush(new BinaryWebSocketFrame(respByteBuf));
    }
    /***
     * 消息发送
     * @author: Admin
     * @date: 2024/4/3 17:12
     * @param channel channel
     * @param b 消息数据
     * @param msgId 消息id
     * @return: void
     */
    public static void sendChannelMsg(Channel channel, byte[] b, int msgId) {
        byte[] b2 = new byte[b.length + 4];
        ByteBuf respByteBuf = Unpooled.wrappedBuffer(b2);
        //从0位开始写
        respByteBuf.writerIndex(0);
        //前4位是消息id
        respByteBuf.writeInt(msgId);
        //响应数据
        respByteBuf.writeBytes(b);
        channel.writeAndFlush(new BinaryWebSocketFrame(respByteBuf));
    }


    /***
     * 旧设备离线|长时间不操作离线
     * @author: Admin
     * @date: 2024/5/8 10:19
     * @param channel channel
     * @return: void
     */
    public static void offlineMsg(Channel channel,String msg) throws InterruptedException {
//        MsgProto.G2C_Login_OfflineResp.Builder offResp = MsgProto.G2C_Login_OfflineResp.newBuilder();
//        offResp.setOffline(true);
//        offResp.setMsg(msg);
//        byte[] b = offResp.build().toByteArray();
//        byte[] b2 = new byte[b.length + 4];
//        ByteBuf respByteBuf = Unpooled.wrappedBuffer(b2);
//        //从0位开始写
//        respByteBuf.writerIndex(0);
//        //前4位是消息id
//        respByteBuf.writeInt(MessageDefine.MSGID_G2C_Login_OfflineResp);
//        //响应数据
//        respByteBuf.writeBytes(b);
//        channel.writeAndFlush(new BinaryWebSocketFrame(respByteBuf)).sync();
    }
}
